package com.anglireports.report.data.common;

import com.alibaba.fastjson.JSONObject;
import com.anglireports.report.bean.primary.Classroom;
import com.anglireports.report.bean.primary.Course;
import com.anglireports.report.bean.primary.Student;
import com.anglireports.report.bean.report.Reports;
import com.anglireports.report.data.base.ResponseData;
import com.anglireports.report.data.base.ResultCodeEnum;
import com.anglireports.report.data.common.angli.AngliCommonData;
import com.anglireports.report.data.common.zhujian.CommonData;
import com.anglireports.report.response.anliResult.ClassModel;
import com.anglireports.report.response.anliResult.OrgModel;
import com.anglireports.report.response.result.ReportsResult;
import com.anglireports.report.response.result.StudentResult;
import com.anglireports.report.response.util.DateUtil;
import com.anglireports.report.response.util.StringUtil;

import java.util.*;

/**
 * @author Administrator
 * @createTime 2019/03/31
 */
public class SelectData {


    /**
     * 获取下拉框校区信息
     * @param starttime
     * @param endtime
     * @return
     */
    public static ResponseData getCampusData(String starttime, String endtime){
        ResponseData responseData = new ResponseData();

        //获取竹间校区信息
        Map<String, Classroom> classroomMap =
                CommonData.getClassroomsMap(DateUtil.strStrToiso(starttime, 0));
        //获取昂立校区信息
        Map<String, OrgModel> orgModelMap = AngliCommonData.getOrgModelMap(DateUtil.formatDate(starttime,
                DateUtil.simpleFormat, DateUtil.dateFormatPay));

        if(classroomMap != null && orgModelMap != null){
            List<Classroom> classeList = new ArrayList<>();
            //循环竹间接口数据获取校区信息判断是否在昂立校区中存在若存在加入列表,不存在则不保存
            Iterator<String> keys = classroomMap.keySet().iterator();
            while (keys.hasNext()){
                String key = keys.next();
                Classroom classroom = classroomMap.get(key);
                if(null != orgModelMap.get(classroom.getOrg_code())){
                    classeList.add(classroom);
                }
            }
            if(classeList.size() == 0){
                responseData.setErr_code(ResultCodeEnum.NO_DATA.getErr_code());
                responseData.setErr_msg(ResultCodeEnum.NO_DATA.getErr_msg());
            } else {
                Map<String,Object> response = new HashMap<>();
                response.put("campus", classeList);
                responseData.setResponse(response);
            }
        } else {
            responseData.setErr_code(ResultCodeEnum.NO_DATA.getErr_code());
            responseData.setErr_msg(ResultCodeEnum.NO_DATA.getErr_msg());
        }
        return responseData;
    }


    /**
     * 获取下拉框班级信息
     * @param starttime
     * @param endtime
     * @return
     */
    public static ResponseData getCoursesData(String starttime, String endtime, String campusid, String orgcode){
        ResponseData responseData = new ResponseData();

        //获取竹间校区信息
        Map<String, Classroom> classroomMap =
                CommonData.getClassroomsMap(DateUtil.strStrToiso(starttime, 0));

        //获取昂立班级信息
        Map<String, ClassModel> classModelMap = AngliCommonData.getClassModelMap(orgcode);

        if(classroomMap != null && classroomMap.get(campusid)!= null && classroomMap.get(campusid).getCourses() != null
                && classModelMap != null){
            List<Course> courseList = new ArrayList<>();
            /**
             * 根据campusid获取竹间校区信息, 并获取该校区下班级列表,循环该班级列表
             * 信息判断是否在昂立该校区班级中存在若存在加入列表,不存在则不保存
             */

            List<Course> courses = classroomMap.get(campusid).getCourses();
            for (Course c : courses){
                if(null != classModelMap.get(c.getClass_id()+"")){
                    courseList.add(c);
                }
            }
            if(courseList.size() == 0){
                responseData.setErr_code(ResultCodeEnum.NO_DATA.getErr_code());
                responseData.setErr_msg(ResultCodeEnum.NO_DATA.getErr_msg());
            } else {
                Map<String,Object> response = new HashMap<>();
                response.put("courses", courseList);
                responseData.setResponse(response);
            }
        } else {
            responseData.setErr_code(ResultCodeEnum.NO_DATA.getErr_code());
            responseData.setErr_msg(ResultCodeEnum.NO_DATA.getErr_msg());
        }
        return responseData;
    }




    /**
     * 获取下拉学生信息
     * @param starttime
     * @param endtime
     * @return
     */
    public static ResponseData getStudentData(String starttime, String endtime, String courseid, Integer classid){
        ResponseData responseData = new ResponseData();

        //获取竹间学生信息
        Set<Student> studentSet =
                getStudentData(DateUtil.strStrToiso(starttime, 0),
                        DateUtil.strStrToiso(endtime, 1), courseid);

        //获取昂立班级信息
        Map<String, Map<String,Object>> stuAndTeaMap = AngliCommonData.getStuAndTeaMap(classid);
        if(studentSet != null && stuAndTeaMap != null){
            List<Student> studentList = new ArrayList<>();
            /**
             * 根据报告中的学生列表信息判断是否在昂立班级中存在若存在加入列表,不存在则不保存
             */

            Map<String,Object> studentMap = stuAndTeaMap.get("studentModelMap");
            if(studentMap != null){
                for (Student s : studentSet){
                    if(null != studentMap.get(s.getPhone_number())){
                        studentList.add(s);
                    }
                }
                if(studentList.size() == 0){
                    responseData.setErr_code(ResultCodeEnum.NO_DATA.getErr_code());
                    responseData.setErr_msg(ResultCodeEnum.NO_DATA.getErr_msg());
                } else {
                    Map<String,Object> response = new HashMap<>();
                    response.put("students", studentList);
                    responseData.setResponse(response);
                }
            } else {
                responseData.setErr_code(ResultCodeEnum.NO_DATA.getErr_code());
                responseData.setErr_msg(ResultCodeEnum.NO_DATA.getErr_msg());
            }
        } else {
            responseData.setErr_code(ResultCodeEnum.NO_DATA.getErr_code());
            responseData.setErr_msg(ResultCodeEnum.NO_DATA.getErr_msg());
        }
        return responseData;
    }


    /**
     * 获取报告中的学生列表
     *courseid
     * @param starttime
     * @param endtime
     * @return
     */
    public static Set<Student> getStudentData(String starttime, String endtime, String course_id) {
        Map<String,Student> studentMap = CommonData.getStudentMap();
        if(studentMap == null){
            return null;
        }
        if (null != starttime && null != endtime & null != course_id) {
            ReportsResult reportsResult = CommonData.getReports(starttime, endtime, course_id, null, null);
            if (reportsResult.getResult_code() == 0) {
                HashMap<String, Object> response = new HashMap<String, Object>();
                List<Reports> reportsList = reportsResult.getReports();
                Set<Student> setStudent = new HashSet<>();

                //获取报告中的学生并根据学生id和班级id去重,将学生信息保存
                for (Reports re : reportsList) {
                    setStudent.add(studentMap.get(re.getStudent_id()));
                }
                return setStudent;
            }
        }
        return null;
    }


    public static void main(String[] args){
        //System.out.println(JSONObject.toJSONString(SelectData.getCampusData("20180101","20180201")));
        System.out.println(JSONObject.toJSONString(
        SelectData.getCoursesData("20180101","20180201","5bda63286a525c640fd12229","EDU201001")));
        //System.out.println(JSONObject.toJSONString(SelectData.getStudentData("20190127","20190329","5c9345c935e1406cbc071939",100)));
    }

}
